package com.example.qihuibao.base

import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.example.qihuibao.utils.DialogUtil

/**
 *     author : cwwei
 *     e-mail : 494545699@qq.com
 *     date   : 2022/08/24
 *     desc   :
 */
abstract class BaseDialogFragment(private val layoutId:Int, private val cancelable:Boolean = true) : DialogFragment() {
    protected var mView: View? = null
    private val fullScreen = false

    abstract fun initView()

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        mView = LayoutInflater.from(activity).inflate(layoutId, null)
        initView()
        return initDialog()
    }

    open fun initDialog() : Dialog {
        val dialog = Dialog(requireActivity())
        isCancelable = cancelable
        dialog.setContentView(mView!!)
        val window = dialog.window
        if (fullScreen) {
            DialogUtil.focusNotAle(dialog.getWindow());
        }
        dialog.show()
        if (fullScreen) {
            DialogUtil.hideNavigationBar(dialog.getWindow());//隐藏导航栏
            DialogUtil.clearFocusNotAle(dialog.getWindow());
        }
        //show 之后设置(重要)
        val attributes = window!!.attributes
        attributes.width = (requireActivity().resources.displayMetrics.widthPixels * 1f).toInt()
        attributes.height = (requireActivity().resources.displayMetrics.heightPixels * 1f).toInt()
//        attributes.y = 0 //改变位置
//        attributes.dimAmount = 0.85f //其它区域阴影透明度
        window.attributes = attributes
        window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))//透明背景
//        if (!AppConfig.screenShot) {
//            window.addFlags(WindowManager.LayoutParams.FLAG_SECURE)//禁止截屏录屏
//        }

        return dialog
    }

    override fun show(manager: FragmentManager, tag: String?) {
        try {
            super.show(manager, tag)
        } catch (ignore: IllegalStateException) {
            //  容错处理,不做操作
        }
    }

    override fun dismissAllowingStateLoss() {
        if (!requireActivity().isFinishing) {
            super.dismissAllowingStateLoss();
        }
    }

}